﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SQLHelper;

public partial class front_Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        bind();
    }

    void bind()
    {
        string str = Convert.ToString(Request["search"]);
        string sql;

        switch (str)
        {
            case "科技时事":
                sql = "select * from news where column_name = '" + str + "'";
                Label1.Text = str;
                getData(sql);
                break;
            case "互联网":
                sql = "select * from news where column_name = '" + str + "'";
                Label1.Text = str;
                getData(sql);
                break;
            case "人工智能":
                sql = "select * from news where column_name = '" + str + "'";
                Label1.Text = str;
                getData(sql);
                break;
            case "虚拟现实":
                sql = "select * from news where column_name = '" + str + "'";
                Label1.Text = str;
                getData(sql);
                break;
            default:
                sql = "select * from news where title like '%" + str + "%'";
                Label1.Text = "<b>'" + str + "'</b>的搜索结果";
                getData(sql);
                break;
        }
    }

    void getData(string str)
    {
        int current_page = Convert.ToInt32(Current.Text);
        DataSet res = DB.GetDataWithQuerySecond(str, "news");
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = res.Tables[0].DefaultView;
        DB.ColseSqlCon();
        ps.AllowPaging = true;
        ps.PageSize = 10;
        Total.Text = ps.PageCount.ToString();
        ps.CurrentPageIndex = current_page - 1;
        First.Enabled = true;
        Up.Enabled = true;
        Down.Enabled = true;
        Last.Enabled = true;
        if (current_page == 1)
        {
            First.Enabled = false;
            Up.Enabled = false;
        }
        if (current_page == Convert.ToInt32(Total.Text))
        {
            Last.Enabled = false;
            Down.Enabled = false;
        }
        DataList1.DataSource = ps;
        DataList1.DataBind();
    }

    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        Current.Text = "1";
        bind();
    }
    protected void lbtnDown_Click(object sender, EventArgs e)
    {
        Current.Text = (Convert.ToInt32(Current.Text) + 1).ToString();
        bind();
    }
    protected void lbntUp_Click(object sender, EventArgs e)
    {
        Current.Text = (Convert.ToInt32(Current.Text) - 1).ToString();
        bind();
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        Current.Text = Total.Text;
        bind();
    }
}